package com.hbyj.struts.action;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.Jbqk01;
import com.hbyj.hbm.vo.YjhsYjhs05;
import com.hbyj.util.Constants;
import com.hbyj.util.PageClass;
import com.opensymphony.xwork2.ActionSupport;

public class YjhsAction  extends ActionSupport{
	
	private File hssp;
	
	private int total;
	private List rows = new ArrayList();
	private String jsonResult;
	
	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}

	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}

	public File getHssp() {
		return hssp;
	}

	public void setHssp(File hssp) {
		this.hssp = hssp;
	}

	
	public void getZhdByKeyword() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String keyword = request.getParameter("keyword");
		String hql = "from Jbqk01 where jbqk0110 like '%"+keyword+"%'";
		List list = DbTools.findByHql(hql);
		if(list.size()>0){
			JSONArray array = new JSONArray();
			for(int i = 0;i<list.size();i++){
				Jbqk01 jbqk = (Jbqk01) list.get(i);
				String id= jbqk.getId().getJbqk0100();
				String name = jbqk.getJbqk0110();
				String biaoshi = jbqk.getId().getJbqk0102();
				JSONObject obj = new JSONObject();
				obj.accumulate("name", name);
				obj.accumulate("id",id);
				obj.accumulate("biaoshi", biaoshi);
				array.add(obj);
			}
			ServletActionContext.getResponse().getOutputStream().write(array.toString().getBytes("utf-8"));
		}
	}
	
	public void insertHscjrk() throws ParseException, UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String time = request.getParameter("time");
		String zlmc = request.getParameter("zlmc");
		String zllb = request.getParameter("zllb");
		String zhdbh = request.getParameter("zhdbh");
		String zhdBiaoshi = request.getParameter("zhdBiaoshi");
		String spFileType = request.getParameter("spFileType");
		//1.查询数据库将组织编号
		String yjhs05210 = queryBiggestYjhs05210ByConditon(zhdbh,zllb);
		//2.将采集的文件上传到指定的目录
		if(hssp!=null){
			String hsspName = changeFileName(zlmc,spFileType);
			if(uploadFile(hsspName)){
				//3.将数据插入到数据库之中
				DateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
				Date date = format.parse(time);
				YjhsYjhs05 yjhs05 = new YjhsYjhs05(null, zhdbh, date, yjhs05210, zlmc, hsspName, "",zhdBiaoshi);
				if(DbTools.saveObj(yjhs05)){
					ServletActionContext.getResponse().getOutputStream().write("true".toString().getBytes("utf-8"));
				}else{
					ServletActionContext.getResponse().getOutputStream().write("false".toString().getBytes("utf-8"));
				}
			}else{
				ServletActionContext.getResponse().getOutputStream().write("false".toString().getBytes("utf-8"));
			}
		}
	}
	
	
	public String queryBiggestYjhs05210ByConditon(String zhdbh,String zllb){
		String yjhs05210 = "";
		String sql = "select YJHS05210 from  YJHS_YJHS05 where  YJHS05100='"+zhdbh+"' and YJHS05210 like 'SP____' order by YJHS05210 DESC";
		List list = DbTools.findBySql(sql);
		if(list.size()>0){
			String biggest = (String) list.get(0);
			String bigNum = biggest.substring(2,6);
			 int newCodeNum = Integer.valueOf(bigNum)+1;
			 String newCode = "";
			 if(newCodeNum<10){
				 newCode = "000"+newCodeNum;
			 }else if(newCodeNum>=10&&newCodeNum<100){
				 newCode = "00"+newCodeNum;
			 }else if(newCodeNum>=100&&newCodeNum<1000){
				 newCode = "0"+newCodeNum;
			 }
			 yjhs05210 = zllb+newCode;
		}else{
			yjhs05210 = zllb+"0001";
		}
		return yjhs05210;
	}
	
	
	//将文件的名字给改变
	public String changeFileName(String zlmc,String spFileType){
		String newFileName = "";
		DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
        String formatDate = format.format(new java.util.Date());
        newFileName  =formatDate+"_"+zlmc+"."+spFileType;
		return newFileName;
	}
	
	//将上传的文件放到指定的目录下面
	public boolean uploadFile(String newFileName){
		String serverPath="uploadfolder/hsfile";
		String getFilePath = ServletActionContext.getServletContext().getRealPath("/"+serverPath);
		if(copyFile(getFilePath, newFileName ,hssp)){
			return true;
		}else{
			return false;
		}
	}
	
	private boolean copyFile(String uploadpath, String uploadFileFileName, File f) {
			String getfilePath = uploadpath;
	        String newfilename = uploadFileFileName;
	        //要存放的文件对象
	        File uploadFile  = new File(getfilePath+"/"+newfilename);
	        // 复制file对象，实现上传 
	        try {
				FileUtils.copyFile(f,uploadFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return true;
	}
	
	
	
	public String queryHszl(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNo = request.getParameter("page");
		int pageSize = Constants.PAGE_KEY;
		String sTime = request.getParameter("sTime");
		String eTime = request.getParameter("eTime");
		String zhCode = request.getParameter("zhCode");
		String zlmc = request.getParameter("zlmc");
		//zhcode肯定有值
		String hql = "from YjhsYjhs05 hs, Jbqk01 jb where jb.id.jbqk0100 = hs.yjhs05100 and jb.id.jbqk0102=hs.jbqk0102 and hs.yjhs05100 like '"+zhCode+"'";
		String countSql = "select count(*) from YjhsYjhs05 hs, Jbqk01 jb where jb.id.jbqk0100 = hs.yjhs05100 and jb.id.jbqk0102=hs.jbqk0102 and hs.yjhs05100 like '"+zhCode+"'";
		if(!("".equals(sTime))){
			hql = hql + " and yjhs05200>'"+sTime+"'";
			countSql = countSql +" and yjhs05200>'"+sTime+"'";
		}
		if(!("".equals(eTime))){
			hql = hql +" and yjhs05200<'"+eTime+"'";
			countSql = countSql +" and yjhs05200<'"+eTime+"'";
		}
		if(!("".equals(zlmc))){
			hql = hql +" and yjhs05220 like '%"+zlmc+"%'";
			countSql = countSql +" and yjhs05200<'"+eTime+"'";
		}
		
		total = DbTools.getDataHQLCount(countSql);
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(total);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		rows = (List)DbTools.findPageHQL(first, max,hql);
		return SUCCESS;
	}
	
}
